﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlaystationNetworkAPI.Core.US
{
    class PlayedGameParser : Common.Parser
    {
        List<string> _HtmlCodeGroups;
        string _HtmlCodeSeparator;
        string _HtmlGamesTrophiesSeparator;

        /// <summary>
        /// TrophiesParser constructor.
        /// </summary>
        /// <param name="htmlCode">HTML code to be handled</param>
        internal PlayedGameParser(string htmlCode)
            : base(htmlCode)
        {
            _HtmlCodeSeparator = Properties.Settings.Default.US_Parser_HtmlCodeSeparator;
            _HtmlGamesTrophiesSeparator = Properties.Settings.Default.US_Parser_HtmlGamesTrophiesSeparator;
            _HtmlCodeGroups = _HtmlCode.Split(new string[] { _HtmlCodeSeparator }, StringSplitOptions.RemoveEmptyEntries).ToList();
        }


        private string GetId(string htmlCodeChunk)
        {
            string collectedUrl = GetAHref("/playstation/psn/profiles", htmlCodeChunk);

            return collectedUrl.Split(Convert.ToChar("/")).Last();
        }

        /// <summary>
        /// Get the game name
        /// </summary>
        /// <param name="htmlCodeChunk">HTML code chunk to be used on the search.</param>
        /// <returns>Game name</returns>
        private string GetName(string htmlCodeChunk)
        {
            return GetInnerText("span", "class", "gameTitleSortField", null, htmlCodeChunk);
        }

        /// <summary>
        /// Get the game image.
        /// </summary>
        /// <param name="htmlCodeChunk">HTML code chunk to be used on the search.</param>
        /// <returns>Full URL for the game image</returns>
        private string GetImage(string htmlCodeChunk)
        {
            return GetImgSource("playstation.net/trophy", htmlCodeChunk);
        }

        /// <summary>
        /// Get the progress in the game
        /// </summary>
        /// <param name="htmlCodeChunk">HTML code chunk to be used on the search.</param>
        /// <returns>The progress in the game</returns>
        private int? GetProgress(string htmlCodeChunk)
        {
            return TryParseNullableInt(GetInnerText("span", "class", "gameProgressSortField", "%", htmlCodeChunk));
        }

        /// <summary>
        /// Get a trophy count.
        /// </summary>
        /// <param name="htmlCodeChunk">HTML code chunk to be used on the search.</param>
        /// <returns>The trophy count</returns>
        private int? GetTrophy(string htmlCodeChunk)
        {
            return TryParseNullableInt(GetInnerText("div", "class", "trophycontent", null, htmlCodeChunk));
        }

        /// <summary>
        /// Get the total number of trophies.
        /// </summary>
        /// <param name="htmlCodeChunk">HTML code chunk to be used on the search.</param>
        /// <returns>The total trophies number.</returns>
        private int? GetTotalTrophies(string htmlCodeChunk)
        {
            return TryParseNullableInt(GetInnerText("span", "class", "gameTrophyCountSortField", null, htmlCodeChunk));
        }

        /// <summary>
        /// Get the trophy count for a game.
        /// </summary>
        /// <param name="htmlCodeChunk">HTML code chunk to be used on the search.</param>
        /// <returns>Complete TrophyCount for a game.</returns>
        private TrophyCount GetTrophyCount(string htmlCodeChunk)
        {
            List<string> trophyHtmlCode = htmlCodeChunk.Split(new string[] { _HtmlGamesTrophiesSeparator }, StringSplitOptions.RemoveEmptyEntries).ToList();

            TrophyCount trophyCount = new TrophyCount();

            trophyCount.Total = GetTotalTrophies(htmlCodeChunk);

            int codeChunkCount = 0;

            foreach (string trophyHtmlCodeChunk in trophyHtmlCode)
            {
                if (!trophyHtmlCodeChunk.Contains("gameTrophyCountSortField"))
                {

                    switch (codeChunkCount)
                    {
                        case 0:
                            trophyCount.Bronze = GetTrophy(trophyHtmlCodeChunk);
                            break;

                        case 1:
                            trophyCount.Silver = GetTrophy(trophyHtmlCodeChunk);
                            break;

                        case 2:
                            trophyCount.Gold = GetTrophy(trophyHtmlCodeChunk);
                            break;

                        case 3:
                            trophyCount.Platinum = GetTrophy(trophyHtmlCodeChunk);
                            break;

                        default:
                            break;
                    }

                    codeChunkCount++;
                }
            }

            return trophyCount;
        }

        /// <summary>
        /// Get the played games for the Playstation Network Profile
        /// </summary>
        /// <returns>Played games</returns>
        internal List<Core.PlayedGame> GetPlayedGames()
        {
            List<Core.PlayedGame> parsedPlayedGames = new List<PlayedGame>();

            foreach (string htmlCodeChunk in _HtmlCodeGroups)
            {
                if (htmlCodeChunk.Contains("gameTitleSortField"))
                {
                    PlayedGame parsedPlayedGame = new PlayedGame()
                    {
                        Id = GetId(htmlCodeChunk),
                        Title = GetName(htmlCodeChunk),
                        Image = GetImage(htmlCodeChunk),
                        Progress = GetProgress(htmlCodeChunk),
                        TrophyCount = GetTrophyCount(htmlCodeChunk)
                    };

                    parsedPlayedGames.Add(parsedPlayedGame);
                }
            }

            return parsedPlayedGames;
        }
    }
}
